<?php
/*
* $Author ：PHPYUN开发团队
*
* 官网: http://www.phpyun.com
*
* 版权所有 2009-2018 宿迁鑫潮信息技术有限公司，并保留所有权利。
*
* 软件声明：未经授权前提下，不得用于商业运营、二次开发以及任何形式的再次发布。
 */
class register_controller extends wxapp_controller{
    function index_action(){
        if($this->config['reg_user_stop']!=1){
            $this->errjson('网站已关闭注册',1);
        }
        $ip = fun_ip_get();
        $UserinfoM=$this->MODEL('userinfo');
        if($this->config['sy_reg_interval']>0 && $this->config['sy_reg_interval']!=''){
            $intervaltime=time()-3600*$this->config['sy_reg_interval'];
            $regnum=$UserinfoM->GetMemberNum(array('reg_ip'=>$ip,"`reg_date`<'".$intervaltime."'"));
            if($regnum){
                $this->errjson('请勿频繁注册',2);
            }
        }
        $_POST=$this->post_trim($_POST);
        if($_POST['username']){
            $username = $_POST['username'];
            if(CheckRegUser($username)==false && CheckRegEmail($username)==false){
                $this->errjson('用户名包含特殊字符',3);
            }
        }
        $usertype=$_POST['usertype'];
        //个人手机必填 || 企业手机必填 || 是手机注册 || 实名认证
        if(($this->config['reg_usertel']=='1' && $usertype=='1')
            || ($this->config['reg_comtel']=='1' && $usertype=='2')
            || $_POST['regway']=='2'
            || $this->config['reg_real_name_check']=="1"
            ){
                if(!CheckMoblie($_POST['moblie'])){
                    $this->errjson('手机格式错误,' . $_POST['moblie'] . '###',4);
                }else{
                    $moblieNum = $UserinfoM->GetMemberNum(array("moblie"=>$_POST['moblie']));
                    if($moblieNum>0){
                        $this->errjson('手机已存在',5);
                    }
                }
        }
        //后台开启注册实名认证 或者 手机注册并且开启了短信验证
        if($this->config['reg_real_name_check']=="1" || $_POST['regway']=='2' && $this->config['sy_msg_regcode']=="1"){
            if($_POST['moblie_code']){
                $regCertMobile = $UserinfoM->GetCompanyCert(array("type"=>'2',"check"=>$_POST['moblie']));
            }
            if($regCertMobile['check2']!=$_POST['moblie_code'] || $regCertMobile['check2']==''){
                $this->errjson('短信验证码错误',6);
            }
        }
        if($usertype == 1){
            if($this->config['reg_username'] == 1){
                if($_POST['name']==""){
                    $this->errjson('请填写真实姓名',7);
                }
                else if(CheckRegUser($_POST['name'])==false){
                    $this->errjson('真实姓名包含特殊字符',8);
                }
            }
        }
        
        if(($this->config['reg_useremail']=='1' && $usertype=='1')
            || ($this->config['reg_comemail']=='1' && $usertype=='2')
            || $_POST['regway'] == 3
            ){
                if($_POST['email'] == ''){
                    $this->errjson('请填写email',9);
                }
                else if(CheckRegEmail($_POST['email'])==false){
                    $this->errjson('Email格式不规范',10);
                }
        }
        if($usertype=='2'){
            if($this->config['reg_comname'] =='1'){
                if($_POST['unit_name']==""){
                    $this->errjson('请正确填写企业名称',11);
                }else{
                    $comnameNum = $this->obj->DB_select_num("company","`name`='".$_POST['unit_name']."'");
                    if($comnameNum>0){
                        $this->errjson('企业名称已被使用',12);
                    }
                }
            }
            if($this->config['reg_comaddress'] =='1'){//企业地址必填
                if($_POST['address']==""){
                    $this->errjson('请正确填写企业地址',13);
                }
            }
            if($this->config['reg_comlink'] =='1'){//企业联系人必填
                if(CheckRegUser($_POST['linkman'])==false || $_POST['linkman']==""){
                    $this->errjson('请正确填写企业联系人',14);
                }
            }
        }
        if($_POST['regway']=='2'){//手机注册
            $username =  $_POST['moblie'];
        }elseif($_POST['regway']=='3'){
            if(CheckRegEmail($_POST['email'])==false || $_POST['email']==""){
                $this->errjson('Email格式不规范',10);
            }
            $username =  $_POST['email'];
        }
        if($username){
            $nid = $UserinfoM->GetMemberNum(array("username"=>$username));
            if($nid){
                $this->errjson('账户名已存在',15);
            }
            
            if($usertype=='1'){
                $satus = $this->config['user_state'];
            }elseif($usertype=='2'){
                $satus = $this->config['com_status'];
            }
            $time=time();
            $salt = substr(uniqid(rand()), -6);
            $pass = md5(md5($_POST['password']).$salt);
            $data=array(
                'username' => $username,
                'password' => $pass,
                'salt' => $salt,
                'usertype' => $usertype,
                'email' => $_POST['email'],
                'moblie' => $_POST['moblie'],
                'did' => $this->config['did'],
                'status' => $satus,
                'reg_date' => $time,
                'reg_ip' => $ip,
                'source' => 13
            );
            $userid=$UserinfoM->AddMember($data);
            if(!$userid){
                $user_id = $UserinfoM->GetMemberOne(array("username"=>$username),array("field"=>"uid"));
                $userid = $user_id['uid'];
            }
            if($userid){
                $getdata=$this->getOpenid($_POST['code']);
                if ($getdata['unionid']){
                    $this->obj->DB_update_all("member","`wxopenid`='',`unionid`=''","`wxopenid`='".$getdata['openid']."' OR (`unionid`='".$getdata['unionid']."' AND `unionid`<>'')");
                    $nid=$this->obj->DB_update_all("member","`wxopenid`='".$getdata['openid']."',`unionid`='".$getdata['unionid']."'","`uid`='".$userid."'");
                }else {
                    $this->obj->DB_update_all("member","`wxopenid`=''","`wxopenid`='".$getdata['openid']."'");
                    $nid=$this->obj->DB_update_all("member","`wxopenid`='".$getdata['openid']."'","`uid`='".$userid."'");
                }
                if($_POST['usertype']=="1"){
                    $table = "member_statis";
                    $table2 = "resume";
                    $data1=array("uid"=>$userid);
                    $data2=array("uid"=>$userid,"email"=>$_POST['email'],"telphone"=>$_POST['moblie'],"name"=>$_POST['name'],'did'=>$this->config['did']);
                    if($_POST['logourl']){
                        $data2['photo']=$data2['resume_photo']=$this->getImage($_POST['logourl'], 'user');
                    }
                }elseif($_POST['usertype']=="2"){
                    $table = "company_statis";
                    $table2 = "company";
                    $data1=$UserinfoM->FetchRatingInfo(array("uid"=>$userid));
                    $data2['uid']=$userid;
                    $data2['linkmail']=$_POST['email'];
                    if($_POST['areacode']&&$_POST['telphone']){
                        $data2['linkphone']=$_POST['areacode'].'-'.$_POST['telphone'];
                    }
                    if($_POST['exten']){
                        $data2['linkphone'].='-'.$_POST['exten'];
                    }
                    $data2['name']=$_POST['unit_name'];
                    $data2['linktel']=$_POST['moblie'];
                    $data2['address']=$_POST['address'];
                    $data2['linkman']=$_POST['linkman'];
                    $data2['did']=$this->config['did'];
                    
                    //添加值班顾问ID
                    $conid = $UserinfoM->Guwen();
                    $data2['conid'] = $conid['conid'];
                    if($conid['crmid']){
                        $data2['crm_uid'] = $conid['crmid'];
                        $data2['crm_time'] = time();
                    }
                    if($satus==0){
                        $data2['r_status']=2;
                    }
                }
                //手机经过验证
                if($_POST['regway']=='2' && $this->config['sy_msg_regcode']=="1" || $this->config['reg_real_name_check']=="1"){
                    //取消该手机之前验证的用户
                    $UserinfoM->UpdateMember(array("moblie"=>''),array("moblie"=>trim($_POST['moblie']),'uid<>'=>$userid));
                    if($usertype == '1'){
                        $data2['moblie_status']="1";
                    }elseif($usertype == '2'){
                        $data2['moblie_status']="1";
                    }
                }
                $data1['did']=$this->config['did'];
                $UserinfoM->InsertReg($table,$data1);
                $UserinfoM->InsertReg($table2,$data2);
                
                $IntegralM=$this->MODEL('integral');
                if($this->config['integral_reg']>0){
                    $IntegralM->company_invtal($userid,$this->config['integral_reg'],true,"注册赠送",true,2,'integral',23);//注册积分赠送
                }
                //如后台开启注册赠送优惠券
                if($this->config['reg_coupon']){
                    $coupon=$this->obj->DB_select_once("coupon","`id`='".$this->config['reg_coupon']."'");
                    $cdata.="`uid`='".$userid."',";
                    $cdata.="`number`='".$time."',";
                    $cdata.="`ctime`='".$time."',";
                    $cdata.="`coupon_id`='".$coupon['id']."',";
                    $cdata.="`coupon_name`='".$coupon['name']."',";
                    $validity=$time+$coupon['time']*86400;
                    $cdata.="`validity`='".$validity."',";
                    $cdata.="`coupon_amount`='".$coupon['amount']."',";
                    $cdata.="`coupon_scope`='".$coupon['scope']."'";
                    $this->obj->DB_insert_once("coupon_list",$cdata);
                }
                if($usertype=="1"){
                    if($this->config['user_status']=="1" && $_POST['email']){
                        $randstr=rand(10000000,99999999);
                        $base=base64_encode($userid."|".$randstr."|".$this->config['coding']);
                        $data_cert['uid']=$userid;
                        $data_cert['type']="cert";
                        $data_cert['did']=$this->config['did'];
                        $data_cert['email']=$_POST['email'];
                        $data_cert['url']="<a href='".$this->config['sy_weburl']."/index.php?m=qqconnect&c=mcert&id=".$base."'>点击认证</a>";
                        $data_cert['date']=date("Y-m-d");
                        if($this->config['sy_email_set']=="1"){
                            $notice = $this->MODEL('notice');
                            $notice->sendEmailType($data_cert);
                            $this->errjson('帐号激活邮件已发送到您邮箱，请先激活',16);
                        }else{
                            $this->errjson('网站暂未开启邮箱功能',17);
                        }
                    }else{
                        $_POST['uid']=$userid;
                        $_POST['username']=$username;
                        $this->regemail($_POST);
                        if($this->config['user_state']!="1"){
                            $this->errjson('注册成功，请等待管理员审核！',18);
                        }else{
                            $IntegralM->get_integral_action($userid,"integral_login","会员登录");
                            $UserinfoM->UpdateMember(array("login_date"=>$time,"login_ip"=>$ip),array("uid"=>$userid));//更新登录时间
                            $result = array(
                                'error' => 0,
                                'errmsg' => '注册成功',
                                'uid' => $userid,
                                'usertype' => $usertype
                            );
                        }
                    }
                }elseif($usertype=="2"){
                    $_POST['uid']=$userid;
                    $_POST['username']=$username;
                    $this->regemail($_POST);
                    if($this->config['com_status']!="1"){
                        $this->errjson('注册成功，请等待管理员审核！',18);
                    }else{
                        $IntegralM->get_integral_action($userid,"integral_login","会员登录");
                        $UserinfoM->UpdateMember(array("login_date"=>$time,"login_ip"=>$ip),array("uid"=>$userid));//更新登录时间
                        $result = array(
                            'error' => 0,
                            'errmsg' => '注册成功',
                            'uid' => $userid,
                            'usertype' => $usertype
                        );
                    }
                }
            }else{
                $this->errjson('注册失败',19);
            }
        }else{
            $this->errjson('请填写用户名',20);
        }
        echo json_encode($result);die;
    }
    //注册发送验证码
    function regcode_action(){
        if($_POST['moblie']==""){
            $this->errjson('请输入手机号',1);
        }
        $randstr=rand(100000,999999);
        
        if($this->config['sy_msguser']==""||$this->config['sy_msgpw']==""||$this->config['sy_msgkey']==""||$this->config['sy_msg_isopen']!='1'){
            $this->errjson('网站暂未开启短信服务',2);
        }else{
            $UserinfoM = $this->MODEL('userinfo');
            $moblieNum = $UserinfoM->GetMemberNum(array("moblie"=>$_POST['moblie']));
            if($moblieNum>0){
                $this->errjson('手机已存在',5);
            }
            $moblieCode = $this->obj->DB_select_once('company_cert',"`check`='".$_POST['moblie']."'");
            if((time()-$moblieCode['ctime'])<120){
                $this->errjson('验证码重复发送，请稍后',3);
            }
            $num=$this->obj->DB_select_num("moblie_msg","`moblie`='".$_POST['moblie']."' and `ctime`>'".strtotime(date("Y-m-d"))."'");
            if($num>=$this->config['moblie_msgnum']){
                $this->errjson('该手机号超过发送条数',4);
            }
            $ip=fun_ip_get();
            $ipnum=$this->obj->DB_select_num("moblie_msg","`ip`='".$ip."' and `ctime`>'".strtotime(date("Y-m-d"))."'");
            if($ipnum>=$this->config['ip_msgnum']){
                $this->errjson('该IP超过一天发送条数',5);
            }
            $notice = $this->MODEL('notice');
            $result = $notice->sendSMSType(array("moblie"=>$_POST['moblie'],"code"=>$randstr,"type"=>'regcode'));
            if($result['status'] != -1){
                $data['did']=$this->config['did'];
                $data['uid']='0';
                $data['type']='2';
                $data['status']='0';
                $data['step']='1';
                $data['check']=$_POST['moblie'];
                $data['check2']=$randstr;
                $data['ctime']=time();
                $data['statusbody']='手机注册验证码';
                if(is_array($moblieCode) && !empty($moblieCode)){
                    $this->obj->update_once("company_cert",$data,"`check`='".$_POST['moblie']."'");
                }else{
                    $this->obj->insert_into("company_cert",$data);
                }
            }
            if($result['msg']){
                $this->errjson($result['msg'],0);die;
            }else{
                $this->errjson('发送失败',6);die;
            }
        }
    }
    //发送注册邮件
    function regemail($post){
        $notice = $this->MODEL('notice');
        if($this->config['sy_email_set']=="1"){
            $notice->sendEmailType(array("username"=>$post['username'],"password"=>$post['password'],"email"=>$post['email'],"cname"=>$post['username'],"type"=>"reg",'uid'=>$post['uid']));
        }
        if($this->config["sy_msguser"]!="" && $this->config["sy_msgpw"]!="" && $this->config["sy_msgkey"]!=""&&$this->config['sy_msg_isopen']=='1'){
            $notice->sendSMSType(array("username"=>$post['username'],"password"=>$post['password'],"moblie"=>$post['moblie'],"type"=>"reg",'uid'=>$post['uid']));
        }
    }
    //下载微信头像
    function getImage($url,$path){
        if (extension_loaded('curl')){
            $CurlReturn=CurlGet($url);
        }elseif(function_exists('file_get_contents')){
            $CurlReturn = file_get_contents($url);
        }
        if ($CurlReturn){
            $filename= time().'.jpg';
            $picDir = date('Ymd', time());
            $dirName = APP_PATH . '/data/upload/'.$path.'/' . $picDir;
            if (!file_exists($dirName)){
                mkdir($dirName, 0777, true);
            }
            
            $res = fopen($dirName . '/' . $filename, 'a');
            fwrite($res, $CurlReturn);
            fclose($res);
            return './data/upload/'.$path.'/' . $picDir.'/'.$filename;
        }else{
            return '';
        }
    }
}
?>